<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/29/13
 * Time: 9:04 PM
 * To change this template use File | Settings | File Templates.
 */

define('APP_ROOT', realpath(__DIR__ . '/../../..'));

require_once(APP_ROOT . '/ext/smarty/Smarty.class.php');
require_once(APP_ROOT . '/modules/persistance/DBConnector.php');
require_once(APP_ROOT . '/modules/log/log.php');
require_once(APP_ROOT . '/modules/log/logCommon.php');
require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/session/SessionModule.php');

use persistance\DBConnector;
use log\Log;
use log\LogCommon;
use common\Errors;
use session\SessionModule;

// Global.
$g_smarty = new Smarty();

function processGet() {
    global $g_smarty;

    Log::LOG_WEBROOTHTML_FB();

    $g_smarty->display('login.tpl');

    Log::LOG_WEBROOTHTML_FE();
}

function processPost() {
    global $g_smarty;

    Log::LOG_WEBROOTHTML_FB();

    if((!isset($_POST['username'])) || (!is_string($_POST['username'])) ||
        (!isset($_POST['password'])) || (!is_string($_POST['password']))) {
        Log::LOG_WEBROOTHTML(LogCommon::logTypeError, "Invalid request parameters.");
        header('HTTP/1.1 400 Bad Request', true, 400);
        return;
    }

    $remoteAddress = $_SERVER['REMOTE_ADDR'];
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $remoteAddress = $remoteAddress . '|' . $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    if(is_null($remoteAddress) || empty($remoteAddress)) {
        $remoteAddress = 'UNKNOWN';
    }

    $userAgent = $_SERVER['USER_AGENT'];
    if(is_null($userAgent) || empty($userAgent)) {
        $userAgent = 'UNKNOWN';
    }

    $smod = new SessionModule();
    $smod->init(new DBConnector());
    $res = $smod->login($_POST['username'],
        $_POST['password'],
        $remoteAddress,
        $userAgent);
    $json = json_decode($res, true);
    if($json['errorCode'] === Errors::INTEGRAL_ERR_COMMON_USER_INVALID_PASSWORD) {
        $g_smarty->assign('error', $json['errorCode']);
        $g_smarty->assign('msg', 'Usuario o contraseña invalida.');
        $g_smarty->display('login.tpl');
    } else if($json['errorCode'] !== Errors::INTEGRAL_ERR_NONE) {
        $g_smarty->assign('error', $json['errorCode']);
        $g_smarty->assign('msg', $json['displayMsg']);
        $g_smarty->display('login.tpl');
    } else {
        header('Location: /html/inventory/index.php');
    }

    Log::LOG_WEBROOTHTML_FE();
}

function main() {
    global $g_smarty;

    Log::LOG_WEBROOTHTML_FB();

    if(($_SERVER['REQUEST_METHOD'] !== 'GET') &&
        ($_SERVER['REQUEST_METHOD'] !== 'POST')) {
        Log::LOG_WEBROOTHTML(LogCommon::logTypeError, "Invalid request method.");
        header('HTTP/1.1 400 Bad Request', true, 400);
        return;
    }

    $g_smarty->setTemplateDir(array(APP_ROOT . '/webroot/html/common/templates',
        APP_ROOT . '/webroot/html/login/templates'));
    $g_smarty->setCompileDir(APP_ROOT . '/cache/html/templates/compiled');
    $g_smarty->setCacheDir(APP_ROOT . '/cache/html/templates/cache');

    if($_SERVER['REQUEST_METHOD'] === 'GET') {
        processGet();
    } else if($_SERVER['REQUEST_METHOD'] === 'POST') {
        processPost();
    }

    Log::LOG_WEBROOTHTML_FE();
}

main();
